package com.qf.sys.service.impl;

import com.qf.entity.sys.SysUser;
import com.qf.exception.CustomerException;
import com.qf.sys.dao.SysUserDAO;
import com.qf.sys.param.LoginParam;
import com.qf.sys.service.SysUserService;
import com.qf.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserDAO sysUserDAO;
    @Autowired
    private RedisUtil redisUtil;
    @Override
    public String login(LoginParam loginParam) {
        SysUser sysUser = sysUserDAO.selectByUserName(loginParam.getUsername());
        if (sysUser == null) {// 用户不存在
            throw new CustomerException("50001","用户不存在");
        }
        if (!(sysUser.getPassword().equals(loginParam.getPassword()))) {
            throw new CustomerException("50002","密码错误");
        }
        //生成一个token，作为唯一的标识
        String token = UUID.randomUUID().toString();
        //将token存到redis中
        redisUtil.setCacheObject(token, sysUser, 60 * 60, TimeUnit.SECONDS);
        return token;
    }
}
